﻿#region Imports
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Web;
using MarketPlace.Data.Bases;
using MarketPlace.Data.DomainModels;
using NHibernate;
using NHibernate.Engine;
using NHibernate.Linq;
#endregion

namespace MarketPlace.Data
{
    ///// <summary>
    ///// 
    ///// </summary>
    //public class SizeComparer : IEqualityComparer<ProductVariant>
    //{
    //    public bool Equals(ProductVariant source, ProductVariant dest)
    //    {

    //        return source.Size == dest.Size;
    //    }

    //    public int GetHashCode(ProductVariant obj)
    //    {
    //        return obj.Size.GetHashCode();
    //    }
    //}

    /// <summary>
    /// 
    /// </summary>
    public class DataContext : NHibernateContext
    {
        public DataContext(ISession session)
            : base(session)
        {
        }

        /// <summary>
        /// 附件
        /// </summary>
        //public IOrderedQueryable<Attachment> Attachments
        //{
        //    get { return Session.Linq<Attachment>(); }
        //}

        /// <summary>
        /// 公司
        /// </summary>
        public IOrderedQueryable<Company> Companies
        {
            get { return Session.Linq<Company>(); }
        }

        /// <summary>
        /// 行业
        /// </summary>
        public IOrderedQueryable<Industry> Industries
        {
            get { return Session.Linq<Industry>(); }
        }

        /// <summary>
        /// 职位
        /// </summary>
        //public IOrderedQueryable<Job> Jobs
        //{
        //    get { return Session.Linq<Job>(); }
        //}

        /// <summary>
        /// 日志
        /// </summary>
        public IOrderedQueryable<Log> Logs
        {
            get { return Session.Linq<Log>(); }
        }

        /// <summary>
        /// 邮件列表选项
        /// </summary>
        //public IOrderedQueryable<MailListSetting> MailListSettings
        //{
        //    get { return Session.Linq<MailListSetting>(); }
        //}

        ///// <summary>
        ///// 消息
        ///// </summary>
        //public IOrderedQueryable<SiteMessage> SiteMessages
        //{
        //    get { return Session.Linq<SiteMessage>(); }
        //}

        /// <summary>
        /// 简历
        /// </summary>
        //public IOrderedQueryable<Resume> Resumes
        //{
        //    get { return Session.Linq<Resume>(); }
        //}

        /// <summary>
        /// 简历
        /// </summary>
        //public IOrderedQueryable<ResumeDoc> ResumeDocs
        //{
        //    get { return Session.Linq<ResumeDoc>(); }
        //}

        public IOrderedQueryable<MarketInfo> MarketInfo
        {
            get
            {
                return Session.Linq<MarketInfo>();
            }
        }

        /// <summary>
        /// 地区
        /// </summary>
        public IOrderedQueryable<Region> Regions
        {
            get { return Session.Linq<Region>(); }
        }

        /// <summary>
        /// 网站广告
        /// </summary>
        //public IOrderedQueryable<SiteAd> SiteAds
        //{
        //    get { return Session.Linq<SiteAd>(); }
        //}

        public IOrderedQueryable<SiteDoc> SiteDocs
        {
            get { return Session.Linq<SiteDoc>(); }
        }

        /// <summary>
        /// 新闻
        /// </summary>
        public IQueryable<News> News
        {
            get { return Session.Linq<News>(); }
        }

        /// <summary>
        /// 独家新闻
        /// </summary>
        public IQueryable<OnlyNews> OnlyNews
        {
            get { return Session.Linq<OnlyNews>(); }
        }

        /// <summary>
        /// 百家争鸣
        /// </summary>
        public IQueryable<AzDoc> AzDocs
        {
            get { return Session.Linq<AzDoc>(); }
        }

        /// <summary>
        /// 行业新闻
        /// </summary>
        public IQueryable<IndustryNews> IndustryNews
        {
            get { return Session.Linq<IndustryNews>(); }
        }

        /// <summary>
        /// 行业新闻
        /// </summary>
        public IQueryable<InternationalNews> InternationalNews
        {
            get { return Session.Linq<InternationalNews>(); }
        }

        /// <summary>
        /// 市场研究
        /// </summary>
        public IQueryable<MarketDoc> MarketDocs
        {
            get { return Session.Linq<MarketDoc>(); }
        }

        /// <summary>
        /// 国内市场
        /// </summary>
        public IQueryable<ChinaMarket> ChinaMarkets
        {
            get { return Session.Linq<ChinaMarket>(); }
        }

        /// <summary>
        /// 国际市场
        /// </summary>
        public IQueryable<InternationalMarket> InternationalMarkets
        {
            get { return Session.Linq<InternationalMarket>(); }
        }

        /// <summary>
        /// 水产病例
        /// </summary>
        public IQueryable<MedicalRecord> MedicalRecords
        {
            get { return Session.Linq<MedicalRecord>(); }
        }

        /// <summary>
        /// 培训基地
        /// </summary>
        public IQueryable<TrainingInfo> TrainingInfos
        {
            get { return Session.Linq<TrainingInfo>(); }
        }

        /// <summary>
        /// 帮助文档
        /// </summary>
        public IOrderedQueryable<HelpDoc> HelpDocs
        {
            get { return Session.Linq<HelpDoc>(); }
        }

        /// <summary>
        /// 系统用户
        /// </summary>
        public IOrderedQueryable<User> Users
        {
            get { return Session.Linq<User>(); }
        }

        /// <summary>
        /// 用户输入选项
        /// </summary>
        //public IOrderedQueryable<UserInputOption> UserInputOptions
        //{
        //    get { return Session.Linq<UserInputOption>(); }
        //}

        #region Help Methods

        public IList<MarketPlace.Data.DomainModels.Region> ResidentPlaceCache
        {
            get
            {
                var _ResidentPlace = HttpContext.Current.Cache["ResidentPlace"];

                if (null == _ResidentPlace)
                {

                    _ResidentPlace = Regions.Where(x => x.ParentId == string.Empty).ToList();
                    ((IList<MarketPlace.Data.DomainModels.Region>)_ResidentPlace).Insert(0, Session.Get<MarketPlace.Data.DomainModels.Region>((Int64)1228));

                    HttpContext.Current.Cache.Add("ResidentPlace", _ResidentPlace, null, System.Web.Caching.Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(10), System.Web.Caching.CacheItemPriority.Default, null);
                }

                return (IList<MarketPlace.Data.DomainModels.Region>)_ResidentPlace;
            }
        }

        public IList<MarketPlace.Data.DomainModels.Region> ProvincesCache
        {
            get
            {
                var _Provinces = HttpContext.Current.Cache["Province"];

                if (null == _Provinces)
                {

                    _Provinces = Regions.Where(x => x.ParentId == string.Empty).ToList();
                    HttpContext.Current.Cache.Add("Province", _Provinces, null, System.Web.Caching.Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(10), System.Web.Caching.CacheItemPriority.Default, null);
                }

                return (IList<MarketPlace.Data.DomainModels.Region>)_Provinces;
            }
        }

        public IList<MarketPlace.Data.DomainModels.Region> CitiesCache
        {
            get
            {
                var _Cities = HttpContext.Current.Cache["Cities"];

                if (null == _Cities)
                {

                    _Cities = Regions.Where(x => x.ParentId == "320000").ToList();
                    HttpContext.Current.Cache.Add("Cities", _Cities, null, System.Web.Caching.Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(10), System.Web.Caching.CacheItemPriority.Default, null);
                }

                return (IList<MarketPlace.Data.DomainModels.Region>)_Cities;
            }
        }

        public IList<MarketPlace.Data.DomainModels.Region> TownsCache
        {
            get
            {
                var _Towns = HttpContext.Current.Cache["_Towns"];

                if (null == _Towns)
                {

                    _Towns = Regions.Where(x => x.ParentId == "320500").ToList();
                    HttpContext.Current.Cache.Add("_Towns", _Towns, null, System.Web.Caching.Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(10), System.Web.Caching.CacheItemPriority.Default, null);

                }

                return (IList<MarketPlace.Data.DomainModels.Region>)_Towns;
            }
        }

        #endregion
    }
}
